# -*- coding: utf-8 -*-
from django.db import models
from django.apps import apps
# Create your models here.


class BarCode(models.Model):
    code = models.CharField(max_length=100)
    title = models.CharField(max_length = 100)


class LabelTarget(models.Model):
    target = models.CharField(verbose_name='目标',max_length=16, blank=True,null=True)
    code = models.CharField(verbose_name='编码',max_length=16, blank=True,null=True)

    class Meta:
        verbose_name = "标签目标"
        verbose_name_plural = verbose_name
        ordering = ["code"]

    def __str__(self):
        return "{}:{}".format(self.target, self.code)

    def save(self, *args, **kwargs):

        super(LabelTarget, self).save(*args, **kwargs)  # Call the "real" save() method.


class LabelSeries(models.Model):
    title = models.CharField(verbose_name='系列',max_length=16, blank=True,null=True)
    code = models.CharField(verbose_name='编码',max_length=16, blank=True,null=True)
    fk_target = models.ForeignKey(
        verbose_name='目标',
        to='LabelTarget',
        blank=True,
        null=True
    )

    class Meta:
        verbose_name = "标签系列"
        verbose_name_plural = verbose_name
        ordering = ["code"]

    def __str__(self):
        return "{}:{}".format(self.title,self.code)

    def save(self, *args, **kwargs):
        super(LabelSeries, self).save(*args, **kwargs)  # Call the "real" save() method.


class Label(models.Model):
    # 将target series 信息编进编码中 多个管理器就好
    title = models.CharField(verbose_name='标签',max_length=16, blank=True,null=True)
    code = models.CharField(verbose_name='编码',max_length=16, blank=True,null=True)
    fk_series = models.ForeignKey(
        verbose_name='系列',
        to='LabelSeries',
        blank=True,
        null=True
    )

    class Meta:
        verbose_name = "标签"
        verbose_name_plural = verbose_name
        ordering = ["code"]

    def __str__(self):
        return "{}:{}".format(self.title, self.code)

    def save(self, *args, **kwargs):
        super(Label, self).save(*args, **kwargs)  # Call the "real" save() method.


class LabelStickGood(models.Model):
    fk_good = models.ForeignKey(
        verbose_name='商品',
        blank=True,
        null=True,
        to='good.Good',
        # on_delete=models.CASCADE
    )
    fk_label = models.ForeignKey(
        verbose_name='标签',
        blank=True,
        null=True,
        to='Label',
        # on_delete=models.CASCADE
    )

    class Meta:
        verbose_name = "商品标签"
        verbose_name_plural = verbose_name

    def __str__(self):
        return "{}:{}".format(self.fk_good, self.fk_label)



    # 不使用many2many，则需要自建表将双方联系起来
    # 如Good描述Entity实体，该Good实例被其他实体通过外键引用
    # GoodInstance.good_goodmultimedia.all() === GoodMultimedia.objects.filter(fk_good = GoodInstance.id)
    # GoodInstance.Labelstickgood_set.all() ===  LabelStickGood.objects.filter(fk_good = GoodInstance.id)
